package haha.life.normal;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class RandomUtils {
    public static String getRandomString(int length){
        String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random=new Random();
        StringBuffer sb=new StringBuffer();
        for(int i=0;i<length;i++){
            int number=random.nextInt(62);
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }

    public static String getDirByName(String sFileName){
        //参数判空
        if (sFileName == "" || sFileName == null){
            return null;
        }

        long randomDirName = 0;

        for (int i = 0; i < sFileName.length(); i++) {
            int item = sFileName.charAt(i);
            String unicode = Integer.toHexString(item);
            long tempLong = Long.parseLong(unicode,16);
            randomDirName += tempLong;
        }
        //res = num%1000
        randomDirName =  randomDirName % 1000;
        return Long.toString(randomDirName);
    }

    /**
     * 返回当前时间的字符串
     * @return 当前时间的指定格式的字符串
     */
    public static String nowStr (String pat){
        if (pat==""){
            pat = "yyyy-MM-dd HH:mm:ss";
        }
        Date now = new Date();
        SimpleDateFormat df = new SimpleDateFormat(pat);
        String nowStr = df.format(now);
        return nowStr;
    }
}
